package com.tjian.fafa.user.util.network;

import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.net.ConnectivityManager;
import android.os.Process;
import android.widget.Toast;

import com.tjian.fafa.user.base.myNetwork.NetworkService;

public class NetworkUtil extends Activity
{
    //检查网络连接，如果没有打开网络连接，提醒打开网络,并退出程序；
    public void checkNetwork()
    {
        ConnectivityManager connManager = (ConnectivityManager) this.getSystemService(CONNECTIVITY_SERVICE);
        if (!connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnected() &&
                !connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected())
        {
            Builder builder = new Builder(NetworkUtil.this);
            builder.setTitle("温馨提示").setMessage("目前网络未开通，请打开网络连接").setPositiveButton("确定", new OnClickListener()
            {
                @Override
                public void onClick(DialogInterface dialog, int which)
                {
                    Process.killProcess(Process.myPid());
                }
            }).create().show();
        }
    }

    //检查网络连接并且提醒打开网络连接；
    public static void checkNetworkAndRemindingOpenNetwork()
    {
        NetworkUtil networkUtil = new NetworkUtil();
        networkUtil.checkNetworkAndRemindingOpenNetworkImpl();
    }

    //检查网络连接并且提醒打开网络连接impl；
    public void checkNetworkAndRemindingOpenNetworkImpl()
    {
        ConnectivityManager connManager = (ConnectivityManager) this.getSystemService(CONNECTIVITY_SERVICE);
        if (!connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnected() &&
                !connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected())
        {
            Toast.makeText(this, "目前网络未开通，请打开网络连接", Toast.LENGTH_LONG).show();
        }
        if (!NetworkService.getInstance().getIsConnected())
        {
            NetworkService.getInstance().closeConnection();
            Toast.makeText(this, "连接服务器失败！", Toast.LENGTH_LONG).show();
        }
    }

}
